// import util from './util'

const qs = require('./qs.js')

function getApiHost() {
  const apiHost = 'https://platform.easys.co/hondaapi';
  // const apiHost = 'http://192.168.1.30:9000';

  return apiHost;
}

function getUrl(url) {
  const apiHost = getApiHost()
  if (url.match(/^http/)) {
    return url;
  }
  const token = wx.getStorageSync('access-token');
  url = appendParams(url, {
    'access-token': token
  })
  return url.indexOf('/') === 0 ? apiHost + url : apiHost + '/' + url;
}

function headerRequest() {
  const headers = {}
  headers['Content-Type'] = 'application/json';
  // headers['content-type'] = 'application/x-www-form-urlencoded'
  return headers;
}

function _formatParams(params) {
  let p = [];
  for (const k in params) {
    if (params[k] !== '' && params[k] !== undefined && params[k] !== null) {
      p.push(k + '=' + encodeURIComponent(params[k]).replace(/'/g, ''));
    }
  }
  return p.join('&');
}

function appendParams(uri, params) {
  if (uri === '') {
    return '';
  }
  const query = _formatParams(params);

  if (query !== '') {
    uri += (-1 === uri.indexOf('?')) ? '?' : '&';
    uri += query;
  }
  return uri;
}

function ajaxRequest(method, url, params, cbSuccess, cbError, hasLoad) {
  const opt = {};
  // const token = wx.getStorageSync('access-token');
  method = method.toUpperCase();
  opt.method = method
  opt.url = getUrl(url)
  opt.header = headerRequest()
  opt.success = (res) => {
    if (res.statusCode == 200 && res.statusCode < 300) {
      if (cbSuccess) {
        cbSuccess(res.data, res)
      }
    }
    // else if (res.statusCode == 401) {
    //   try {
    //     wx.removeStorageSync('access-token')
    //   } catch (e) {
    //     wx.getLogManager().debug(e)
    //   }
    //   wx.showModal({
    //     title: '提示',
    //     content: "授权失败了，请重新登录",
    //     success: function(res) {
    //       if (res.confirm) {
    //         // wx.reLaunch({
    //         //   url: '/pages/exhibition-loading/exhibition-loading'
    //         // })
    //       } else if (res.cancel) {
    //         console.log('用户点击取消')
    //       }
    //     }
    //   })
    // } else if (res.statusCode > 500) {
    //   wx.showModal({
    //     title: '提示',
    //     content: "服务器繁忙，请重试",
    //     success: function(res) {
    //       if (res.confirm) {
    //         // wx.reLaunch({
    //         //   url: '/pages/exhibition-loading/exhibition-loading'
    //         // })
    //       }
    //     }
    //   })
    // } 
    else {
      if (cbError) {
		  
        cbError(res.data)
      }
      console.log(res, '错误提示')
    }
  }
  opt.fail = (res) => {
    if (cbError) {
      cbError(res)
    }
  }

  if (method === 'GET' || method === 'DELETE') {
    if (params !== null) {
      opt.data = params
      // opt.data['access-token'] = token === '' ? 'null' : token
    }
  } else if (method === 'POST' || method === 'PATCH' || method === 'PUT') {
    // opt.url = getUrl(url)
    if (params !== null) {
      // params['access-token'] = token
      opt.data = qs.stringify(params)
      if (!opt.data) {
        opt.data = {}
      }
    }
  }

  wx.request(opt)

}

export default {
  post(url, params, cbSuccess, cbError, hasLoad) {
    return ajaxRequest('POST', url, params, cbSuccess, cbError, hasLoad)
  },
  get(url, params, cbSuccess, cbError, hasLoad) {
    return ajaxRequest('GET', url, params, cbSuccess, cbError, hasLoad)
  },
  patch(url, params, cbSuccess, cbError, hasLoad) {
    return ajaxRequest('PATCH', url, params, cbSuccess, cbError, hasLoad)
  },
  put(url, params, cbSuccess, cbError, hasLoad) {
    return ajaxRequest('PUT', url, params, cbSuccess, cbError, hasLoad)
  },
  delete(url, params, cbSuccess, cbError, hasLoad) {
    return ajaxRequest('DELETE', url, params, cbSuccess, cbError, hasLoad)
  },
  // postMethod(url, params) {
  //   return axios.post(url, params, {
  //     baseURL: 'http://api.artigit.com'
  //   })
  // }
}